package com.xmd.red.dao;

import com.xmd.red.bean.RedPacket;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * Created by superbone on 2016/11/1.
 */
@Repository
public class RedJdbcDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    public int count(){

        String sql = "select count(*) from wx_packet_record t where t.state<>2";
        List<Integer> query = jdbcTemplate.query(sql, new RowMapper<Integer>() {
            @Override
            public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
                return rs.getInt(1);
            }
        });
        return query.get(0);
    }

    public List<RedPacket> query(int start,int num){

        String sql = "select * from wx_packet_record t where t.state<>2 limit ?,?";
        List<RedPacket> queryList = jdbcTemplate.query(sql, new RowMapper<RedPacket>() {
            @Override
            public RedPacket mapRow(ResultSet rs, int rowNum) throws SQLException {
                RedPacket red = new RedPacket();
                red.setDeviceId(rs.getString("device_id"));
                red.setDeviceType(rs.getString("device_type"));
                red.setOpenId(rs.getString("open_id"));
                red.setUserId(rs.getInt("user_id"));
                red.setCreateTime(new Date(rs.getTimestamp("create_time").getTime()));
                red.setTotal(rs.getBigDecimal("total").doubleValue());
                red.setState(rs.getInt("state"));
                red.setLastUpdateTime(new Date(rs.getTimestamp("last_update_time").getTime()));
                red.setErrCode(rs.getString("err_code"));
                red.setErrCodeDes(rs.getString("err_code_des"));
                red.setClientIp(rs.getString("client_ip"));
                red.setServerIp(rs.getString("server_ip"));
                red.setFeedback(rs.getInt("feedback"));
                return red;
            }
        },start,num);
        return queryList;
    }
}
